# -*- coding: utf-8 -*
from igt_push import *
from igetui.template.igt_transmission_template import *
from igetui.igt_message import *
from igetui.igt_target import *
from payload.APNPayload import *

import requests

#1：指定推送    2：全部推送
marker=1


adress = "http://meteo.awit.net.cn:10280/GuiYang_weather_BS/gexin.do?fetchPushData"
p1="admin"
p2="awit1024"

url=adress+"&un="+p1+"&pw="+p2


#使用requests请求
rsp = requests.request("get",url)
rspj = rsp.json()
code = rspj['code']
# code = 0
title = '灾害提醒'
msgBody=''
#list长度
messageNum = len(rspj['obj']['list'])
#cid长度
cidsLen = 0

content = {'content':msgBody,'title':title}
allMsg = json.dumps(content)

APPKEY = "tYAFMNRpOP92GCRaLQLyc7"
APPID = "QiAUPEVhaR6Wx22tkdOi06"
MASTERSECRET = "w2WYljJJPA9aW7iIUuCR29"

CID1='66a8704768dad535d014b8c7b91f251f' #hy ios
CID2='9c3a16705e1f88b1a3124e6a02e0c6ec' #wk HTC
CID3='b749ef27b9e5872d4e5f2144b660b5fe'
CID4='e6535bf1658785efbcd94456b3ae3745'
CID5='1a92d4774c0869e84672c8bfd8895c58' #luhang ios
CID6='b749ef27b9e5872d4e5f2144b660b5fe' #zk ios

CID = "b749ef27b9e5872d4e5f2144b660b5fe"

HOST = 'http://sdk.open.api.igexin.com/apiex.htm'

#透传模板动作内容
def TransmissionTemplateDemo():
    template = TransmissionTemplate()
    template.transmissionType = 1
    template.appId = APPID
    template.appKey = APPKEY
    template.transmissionContent = allMsg
    # iOS setAPNInfo
    apnpayload = APNPayload()
    apnpayload.badge =0
    apnpayload.sound = "sound"
    apnpayload.addCustomMsg("payload", "payload")
    apnpayload.contentAvailable = 1
    apnpayload.category = "ACTIONABLE"

    alertMsg = DictionaryAlertMsg()
    alertMsg.body = msgBody
    # alertMsg.body = 'body'
    alertMsg.actionLocKey = 'actionLockey'
    alertMsg.locKey = 'lockey'
    alertMsg.locArgs=['locArgs']
    alertMsg.launchImage = 'launchImage'
    # iOS8.2以上版本支持
    alertMsg.title = title
    alertMsg.titleLocArgs = ['TitleLocArg']
    alertMsg.titleLocKey = 'TitleLocKey'
    apnpayload.alertMsg=alertMsg
    template.setApnInfo(apnpayload)

    return template

def pushMessageToApp():
    push = IGeTui(HOST, APPKEY, MASTERSECRET,True)


    template = TransmissionTemplateDemo()
    #定义"AppMessage"，设置是否离线，离线有效时间，推送模板，推送速度等
    message = IGtAppMessage()
    message.data = template
    message.isOffline = True
    message.offlineExpireTime = 1000 * 3600 * 12
    message.appIdList.extend([APPID])
    ret = push.pushMessageToApp(message)
    # print ret


def pushMessageToList():
    push = IGeTui(HOST, APPKEY, MASTERSECRET,True)

    template = TransmissionTemplateDemo()

    message = IGtListMessage()
    message.data = template
    message.isOffline = True
    message.offlineExpireTime = 1000 * 3600 * 12
    message.pushNetWorkType = 0

    target1 = Target()
    target1.appId = APPID
    target1.clientId = CID5

    arr = []

    arr.append(target1)
    #arr.append(target2)
    contentId = push.getContentId(message)
    # print '####'+contentId
    ret = push.pushMessageToList(contentId, arr)
    # print ret

def pushMessageToSomebody():
    push = IGeTui(HOST, APPKEY, MASTERSECRET,True)
    count = 0
    
    
    while count<messageNum:
        cidsLen = len(rspj['obj']['list'][count]['cids'])
        #通知内容
        msg = rspj['obj']['list'][count]['cnt']
        #路线ID，APP需要
        rid = rspj['obj']['list'][count]['road']['id']
        content1 = {'content':msg,'title':title}
        allMsg1 = json.dumps(content1)
        
        template = TransmissionTemplate1(allMsg1,msg)
        message = IGtListMessage()
        message.data = template
        message.isOffline = True
        message.offlineExpireTime = 1000 * 3600 * 12
        message.pushNetWorkType = 0
        
        if(cidsLen!=0):
            ccount = 0 
            arr = []
            while ccount<cidsLen:
                target = Target()
                target.appId = APPID
                tcid=rspj['obj']['list'][count]['cids'][ccount]
                # if( tcid == CID1 or tcid == CID5 or tcid == CID6):
                target.clientId = rspj['obj']['list'][count]['cids'][ccount]
                arr.append(target)
                contentId = push.getContentId(message)
                    # print '####' + contentId
                    # ret = push.pushMessageToList(contentId, arr)
                    # print ret
                ccount=ccount+1
            ret = push.pushMessageToList(contentId, arr)
        count=count+1

def TransmissionTemplate1(allMsg,msgBody):
    template = TransmissionTemplate()
    template.transmissionType = 1
    template.appId = APPID
    template.appKey = APPKEY
    template.transmissionContent = allMsg
    # iOS setAPNInfo
    apnpayload = APNPayload()
    apnpayload.badge =0
    apnpayload.sound = "sound"
    apnpayload.addCustomMsg("payload", "payload")
    apnpayload.contentAvailable = 1
    apnpayload.category = "ACTIONABLE"

    alertMsg = DictionaryAlertMsg()
    alertMsg.body = msgBody
    # alertMsg.body = 'body'
    alertMsg.actionLocKey = 'actionLockey'
    alertMsg.locKey = 'lockey'
    alertMsg.locArgs=['locArgs']
    alertMsg.launchImage = 'launchImage'
    # iOS8.2以上版本支持
    alertMsg.title = title
    alertMsg.titleLocArgs = ['TitleLocArg']
    alertMsg.titleLocKey = 'TitleLocKey'
    apnpayload.alertMsg=alertMsg
    template.setApnInfo(apnpayload)

    return template

if code == 0:
    if marker == 1:
        
        pushMessageToSomebody()
   
    else:
        pushMessageToApp()